package cn.ljy.uem.service.impl;

import cn.ljy.authority.config.security.UserUtil;
import cn.ljy.authority.model.SysUser;
import cn.ljy.common.exception.model.MyForbiddenException;
import cn.ljy.common.exception.model.MyWebException;
import cn.ljy.common.exception.model.base.MyRollBackException;
import cn.ljy.common.utils.file.MyFileModel;
import cn.ljy.uem.dao.*;
import cn.ljy.uem.model.SchoolCourse;
import cn.ljy.uem.model.SchoolTeacher;
import cn.ljy.uem.service.SchoolTeacherCourseService;
import cn.ljy.uem.service.common.CommonSchoolService;
import cn.ljy.uem.utils.file.UemFileUploadUtil;
import com.github.xiaoymin.knife4j.core.util.CollectionUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.multipart.MultipartFile;

import java.util.ArrayList;
import java.util.List;

@Service
@Validated
@Transactional(propagation = Propagation.REQUIRED,isolation = Isolation.DEFAULT,timeout=36000,rollbackFor= MyRollBackException.class)
@Slf4j
public class ISchoolTeacherCourseService implements SchoolTeacherCourseService {
    @Autowired
    private CommonSchoolService commonSchoolService;
    @Autowired
    private SchoolTeacherCourseMapper schoolTeacherCourseMapper;
    @Autowired
    private SchoolTeacherMapper schoolTeacherMapper;
    @Autowired
    private SchoolMajorMapper schoolMajorMapper;
    @Autowired
    private SchoolDepartmentMapper schoolDepartmentMapper;
    @Autowired
    private SchoolCourseMapper schoolCourseMapper;
    @Autowired
    private UemFileUploadUtil uemFileUploadUtil;

    /**
     * 验证教师是否拥有该课程的管理权限
     * @param courseId
     */
    private void validateManageCourseAuthorityOfLoginAuthority(Long courseId){
        //获取当前登录信息
        SysUser sysUser = UserUtil.getLoginUser();
        if (sysUser != null) {//如果当前登录账户存在
            //获取当前登录账户对应的教师信息
            Long teacherId = schoolTeacherMapper.getIdByUserId(sysUser.getId());
            if(schoolTeacherCourseMapper.countByCourseIdAndTeacherId(courseId,teacherId)==0){
                throw new MyForbiddenException("操作失败：您没有该课程的管理权限");
            }
        }
    }

    @Override
    public List<SchoolCourse> getManageCoursesOfLoginTeacher() {
        List<SchoolCourse> list = new ArrayList<>();
        //获取当前登录信息
        SysUser sysUser = UserUtil.getLoginUser();
        if (sysUser != null) {//如果当前登录账户存在
            //获取当前登录账户对应的教师信息
            SchoolTeacher schoolTeacher = schoolTeacherMapper.getTeacherByUserId(sysUser.getId());
            if (schoolTeacher != null) {
                //查找教师负责的课程
                List<SchoolCourse> courseList = schoolTeacherMapper.getCourseByTeacherId(schoolTeacher.getId());
                //将废弃的课程移除
//                courseList=courseList.stream().filter(course->!course.getExpired()).collect(Collectors.toList());
                if (CollectionUtils.isEmpty(list)) {
                    throw new MyWebException("该教师并无课程");
                }
            } else {
                throw new MyWebException("操作失败：当前账户不是教师账户，无权操作！");
            }
        }
        //查找教师信息下对应的课程
        return list;
    }

    @Override
    public List<SchoolCourse> getCourseAndCourseOfTeachingtaskOfLoginTeacher() {
        List<SchoolCourse> courseList = new ArrayList<>();
        //获取当前登录信息
        SysUser sysUser = UserUtil.getLoginUser();
        if (sysUser != null) {//如果当前登录账户存在
            //获取当前登录账户对应的教师信息
            SchoolTeacher schoolTeacher = schoolTeacherMapper.getTeacherByUserId(sysUser.getId());
            if (schoolTeacher != null) {
                //查找教师负责的课程
                courseList.addAll(schoolTeacherCourseMapper.findCourseAndCourseOfTeachingtaskByTeacherId(schoolTeacher.getId()));
                for (SchoolCourse course : courseList){//添加课程所属负责人信息
                    if (course.getTeacherId()!=null){
                        course.setSchoolTeacher(schoolTeacherMapper.findById(course.getTeacherId()));
                    }
                    if (course.getMajorId()!=null){
                        course.setSchoolMajor(schoolMajorMapper.findById(course.getMajorId()));
                    }
                    if (course.getDepartmentId()!=null){
                        course.setSchoolDepartment(schoolDepartmentMapper.findById(course.getDepartmentId()));
                    }
                    if (course.getCover()!=null){
                        course.setCover("https://tveti.lcvc.edu.cn/"+course.getCover());
//                        course.setCover("http://localhost:8071/"+course.getCover());
//                        System.out.println(course.getCover());
                    }
                }
            } else {
                throw new MyWebException("操作失败：当前账户不是教师账户，无权操作！");
            }
        }
        //查找教师信息下对应的课程
        return courseList;
    }

    @Override
    public String uploadCoverOfCourse(Long courseId, MultipartFile file) {
        //先验证是否有该课程的管理权限
        //验证当前登录账户是否有该课程的权限
        commonSchoolService.validateCourseManageAuthorityOfLoginTeacher(courseId);
        // 上传图片，并获取上传成功后的头像图片网址
        MyFileModel myFileModel=uemFileUploadUtil.uploadResourceOfPicture(file);
        // 存储到数据库
        SchoolCourse schoolCourse = new SchoolCourse(courseId);
        schoolCourse.setCover(myFileModel.getFileRelativeUrl());
        schoolCourseMapper.update(schoolCourse);
        //获取上传图片的完整url路径;
        return myFileModel.getFileUrl();
    }
}
